1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.sun.media.sound;
26
27
28
29
30
31
32 public class SoftLowFrequencyOscillator implements SoftProcess {
33
34 private int max_count = 10;
35 private int used_count = 0;
36 private double[][] out = new double[max_count][1];
37 private double[][] delay = new double[max_count][1];
38 private double[][] delay2 = new double[max_count][1];
39 private double[][] freq = new double[max_count][1];
40 private int[] delay_counter = new int[max_count];
41 private double[] sin_phase = new double[max_count];
42 private double[] sin_stepfreq = new double[max_count];
43 private double[] sin_step = new double[max_count];
44 private double control_time = 0;
45 private double sin_factor = 0;
46 private static double PI2 = 2.0 * Math.PI;
47
48 public SoftLowFrequencyOscillator() {
49
50 for (int i = 0; i < sin_stepfreq.length; i++) {
51 sin_stepfreq[i] = Double.NEGATIVE_INFINITY;
52 }
53 }
54
55 public void reset() {
56 for (int i = 0; i < used_count; i++) {
57 out[i][0] = 0;
58 delay[i][0] = 0;
59 delay2[i][0] = 0;
60 freq[i][0] = 0;
61 delay_counter[i] = 0;
62 sin_phase[i] = 0;
63
64 sin_stepfreq[i] = Double.NEGATIVE_INFINITY;
65 sin_step[i] = 0;
66 }
67 used_count = 0;
68 }
69
70 public void init(SoftSynthesizer synth) {
71 control_time = 1.0 / synth.getControlRate();
72 sin_factor = control_time * 2 * Math.PI;
73 for (int i = 0; i < used_count; i++) {
74 delay_counter[i] = (int)(Math.pow(2,
75 this.delay[i][0] / 1200.0) / control_time);
76 delay_counter[i] += (int)(delay2[i][0] / (control_time * 1000));
77 }
78 processControlLogic();
79 }
80
81 public void processControlLogic() {
82 for (int i = 0; i < used_count; i++) {
83 if (delay_counter[i] > 0) {
84 delay_counter[i]--;
85 out[i][0] = 0.5;
86 } else {
87 double f = freq[i][0];
88
89 if (sin_stepfreq[i] != f) {
90 sin_stepfreq[i] = f;
91 double fr = 440.0 * Math.exp(
92 (f - 6900.0) * (Math.log(2) / 1200.0));
93 sin_step[i] = fr * sin_factor;
94 }
95
96
97
98
99
100
101
102
103
104
105
106 double p = sin_phase[i];
107 p += sin_step[i];
108 while (p > PI2)
109 p -= PI2;
110 out[i][0] = 0.5 + Math.sin(p) * 0.5;
111 sin_phase[i] = p;
112
113 }
114 }
115 }
116
117 public double[] get(int instance, String name) {
118 if (instance >= used_count)
119 used_count = instance + 1;
120 if (name == null)
121 return out[instance];
122 if (name.equals("delay"))
123 return delay[instance];
124 if (name.equals("delay2"))
125 return delay2[instance];
126 if (name.equals("freq"))
127 return freq[instance];
128 return null;
129 }
130 }